#!/bin/sh
web_stat_log=service_monitor.log
mails=$1
if [ ! -f $web_stat_log ]; then
   touch $web_stat_log
fi
server_list_file=service_server.list
if [ ! -f $server_list_file ]; then
   echo "`date '+%Y-%m-%d %H:%M:%S'` ERROR:$server_list_file NOT exists!" >>$web_stat_log
exit 1
fi
#total=`wc -l $server_list_file|awk '{print $1}'`
for website in `cat $server_list_file`
do
   url="http://$website/"
   server_status_code=`curl -I -m 10 -o /dev/null -s -w %{http_code} "$url"`
   if [ "$server_status_code" = "200" ]; then 
        echo "`date '+%Y-%m-%d %H:%M:%S'` visit $website status code 200 OK" >>$web_stat_log
   else 
        echo "`date '+%Y-%m-%d %H:%M:%S'` visit $website error!!! server can't connect at 10s or stop response at 10 s, send mail ..." >>$web_stat_log
        echo "`date '+%Y-%m-%d %H:%M:%S'` visit $website error!!! server can't connect at 10s or stop response at 10 s" | mail -s "monitor $website" $mails
   fi
done
exit 0
